package com.easyse.servicetechqa.rest;

import com.easyse.common.constants.TechqaConstant;
import com.easyse.common.utils.RedisKeyUtil;
import com.easyse.elasticsearch.service.ElasticsearchService;
import com.easyse.kafka.entity.Event;
import com.easyse.kafka.event.EventProducer;
import com.easyse.model.DO.techqa.Techqa;
import com.easyse.model.vo.ResultVO;
import com.easyse.serviceclient.clients.TechqaClient;
import com.easyse.servicetechqa.service.TechqaService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import static com.easyse.common.constants.Constant.ENTITY_TYPE_TECHQA;
import static com.easyse.kafka.util.KafkaConstant.TOPIC_DELETE;
import static com.easyse.kafka.util.KafkaConstant.TOPIC_PUBLISH;

/**
 * @author: zky
 * @date: 2022/10/23
 * @description:
 */
@RestController
@RequestMapping("/techqa")
@Api("技术问答接口")
public class TechqaResource implements TechqaConstant, TechqaClient {


    @Autowired
    TechqaService techqaService;

    @Autowired
    ElasticsearchService elasticsearchService;

    @Autowired
    EventProducer eventProducer;

    @Autowired
    RedisTemplate redisTemplate;

    /**
     * 添加技术问答
     * @param techqa
     * @return
     */
    @PostMapping("/add")
    @ApiOperation(value = "添加技术问答：title、content非空")
    public ResultVO addTechqa(@RequestBody @Validated Techqa techqa) {
        techqaService.save(techqa);

        // TODO 触发事件、通过消息队列存入ES服务器
        Event event = new Event()
                .setTopic(TOPIC_PUBLISH)
                .setUserId(techqa.getUserId())
                .setEntityType(ENTITY_TYPE_TECHQA)
                .setEntityId(techqa.getId());
        eventProducer.fireEvent(event);
        // TODO 计算分数

        return ResultVO.success("发布技术问答成功！");
    }

    /**
     * 删除帖子
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据ID删除技术问答")
    public ResultVO deleteTechqa(@PathVariable("id")Long id, Long userid) {
        techqaService.updateStatus(id, TECHQA_DELETED);

        // TODO 触发删帖事件，通过消息队列更新 Elasticsearch 服务器
        Event event = new Event()
                .setTopic(TOPIC_DELETE)
                .setUserId(userid)
                .setEntityType(ENTITY_TYPE_TECHQA)
                .setEntityId(id);
        eventProducer.fireEvent(event);

        return ResultVO.success("删除成功！");
    }

    /**
     * 置顶技术问答
     * @param id
     * @return
     */
    @PostMapping("/top")
    @ResponseBody
    @ApiOperation(value = "置顶加精技术问答", notes = "0-普通; 1-置顶")
    public ResultVO updateTop(Long id, int type, Long userid) {
        techqaService.updateType(id, type);

        // 触发发帖事件，通过消息队列将其存入 Elasticsearch 服务器
        Event event = new Event()
                .setTopic(TOPIC_PUBLISH)
                .setUserId(userid)
                .setEntityType(ENTITY_TYPE_TECHQA)
                .setEntityId(id);
        eventProducer.fireEvent(event);

        return ResultVO.success("置顶成功");
    }


    /**
     * 加精技术问答
     * @param id
     * @return
     */
    @PostMapping("/wonderful")
    @ResponseBody
    @ApiOperation(value = "加精技术问答", notes = "0-正常; 1-精华; 2-拉黑")
    public ResultVO setWonderful(Long id, Long userid) {
        techqaService.updateStatus(id, 1);

        // 触发发帖事件，通过消息队列将其存入 Elasticsearch 服务器
        Event event = new Event()
                .setTopic(TOPIC_PUBLISH)
                .setUserId(userid)
                .setEntityType(ENTITY_TYPE_TECHQA)
                .setEntityId(userid);
        eventProducer.fireEvent(event);

        // 计算帖子分数
        String redisKey = RedisKeyUtil.getPostScoreKey();
        redisTemplate.opsForSet().add(redisKey, id);

        return ResultVO.success("加精成功");
    }


    @GetMapping("{id}")
    @Override
    public Techqa getById(@PathVariable("id") Long id) {
        return techqaService.getById(id);
    }
}
